package cn.zhouchao.org.controller;

import cn.zhouchao.basic.ann.PreAuthorize;
import cn.zhouchao.basic.jwt.UserInfo;
import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.LoginContext;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.org.domain.Department;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.query.DepartmentQuery;
import cn.zhouchao.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
@RestController
组合注解：返回json格式
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
 */

/**
 * restful风格
 * 部门管理
 */
@RestController// 组合注解：返回json格式
@RequestMapping("/dept")
@Api(value = "部门管理系统",description = "部门的CRUD")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     *查询所有对象
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有对象")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 新增或修改单个对象
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增或修改单个对象")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
            if (null==department.getId()){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
        return AjaxResult.me().success();
    }

    /**
     * 删除单个对象
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个对象")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query, HttpServletRequest req){
        UserInfo userInfo = LoginContext.getUserInfo(req);//通过前端请求后端接口，测试成功！
        System.out.println("测试获取请求里面的用户数据"+userInfo);//通过前端请求后端接口，测试成功！
        return departmentService.queryByPage(query);
    }

    /**
     * 批量删除多个对象
     * 携带的时ids集合，作为对象放在RequestBody请求体中
     * 故与删除不同，就不在路径上+参数，而是通过请求体携带对象（多个）
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除多个对象")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 查询父级部门（逻辑代码是实现）
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "查询父级部门")
//    @PreAuthorize(name = "无限极树", sn = "dept:tree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * findEmpByDeptId
     */
    @GetMapping("/findEmpByDeptId/{id}")
    public List<Employee> findEmpByDeptId(@PathVariable("id")  Long id){
        return departmentService.findEmpByDeptId(id);
    }

}
